<?php

/*
 * File modules.php for project Rijlesplanner.nl
 * Created on 19 nov 2007 at 21:49:56 by Ivar Pruijn
 */

class modules {

	// Properties:
	protected $name = "module";
	protected $title = "Module";
	protected $subtitle = "";
	protected $moduleOptions = array ();
	public $auth;
	public $config;
	public $logger;
	public $user;
	public $template;
	public $wsclient;

	// Methods:

	/**
	 * void __construct ()
	 * This is the constructor method for the User class.
	 */
	public function __construct() {
		global $logger;
		$logger->log("Constructing class " . $this->name . "\n", PEAR_LOG_INFO);
	}

	public function process($template) {
		// process whatever actions have been postedz
	}

	/**
	 * void configure ([array attributes])
	 * It passes its arguments to the ConstructFromArray method to set other properties for the object
	 */
	public function configure(array $atts = NULL) {
		$this->ConstructFromArray($atts);
	}

	public function actionAccessDenied() {
		$this->err[] = array (
			"desc" => "You are not allowed to perform this action"
		);
		$this->template->assign('err', $this->err);
		$this->template->assign('moduleTemplate', "common/action_accessdenied.tpl");
	}

	public function optionAccessDenied() {
		$this->err[] = array (
			"desc" => "You are not allowed to access this option"
		);
		$this->template->assign('err', $this->err);
		$this->template->assign('moduleTemplate', "common/option_accessdenied.tpl");
	}

	public function loadModule($module="",$template=null,$method="") {
		if (file_exists("modules/" . $module . ".php")) {
			require_once ($this->config['module_dir'] . "/" . $module . ".php");
			$modObj = new $module ();
			$modObj->configure(array (
				"config" => $this->config,
				"user" => $this->user,
				"wsclient" => $_SESSION['wsclient'],
				"auth" => $this->auth,
				"template" => $template,
				"logger" => $this->logger
			));
			if(!empty($method)) {
				$modObj->$method();
			} else {
				$modObj->process($template);
			}
		}
	}

	public function getTitle() {
		return $this->title;
	}

	public function getSubTitle() {
		if (!empty ($_GET['option'])) {
			$subtitle = "";
			foreach ($this->moduleOptions AS $mOpt) {
				if ($mOpt['name'] == $_GET['option']) {
					$subtitle = $mOpt['title'];
				}
			}
			return $subtitle;
		} else {
			return $this->subtitle;
		}
	}

	public function setSubTitle($newVal = "") {
		if (!empty ($newVal)) {
			$this->subtitle = $newVal;
		}
	}

	public function getLogger() {
		return $this->logger;
	}

	public function getConfig() {
		return $this->config;
	}

	public function getUser() {
		return $this->user;
	}

	public function getTemplate() {
		return $this->template;
	}

	public function getWsclient() {
		return $this->wsclient;
	}
	
	public function getAuth() {
		return $this->auth;
	}

	public function addError($description = "") {
		$this->err[] = array (
			"desc" => $description
		);
	}

	public function addWarning($description = "") {
		$this->warn[] = array (
			"desc" => $description
		);
	}

	/**
	 * void ConstructFromArray ([array attributes])
	 * This method, if an array is given as the argument, will
	 * create properties in the current object named after the
	 * keys of the array, and set the properties to the corresponding
	 * values from the array.
	 */
	public function ConstructFromArray(array $atts = NULL) {
		if (is_array($atts)) {
			while (list ($key, $value) = each($atts)) {
				$this-> $key = $value;
			}
		}
	}

	public function __destruct() {
		global $logger;
		$logger->log("Destroying class " . $this->name . "\n", PEAR_LOG_INFO);
	}

	public function __toString() {
		return $this->title;
	}

}
?>
